<?php

namespace App\Models;

use AlibabaCloud\Alidns\Alidns;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use Dcat\Admin\Form;
use Dcat\Admin\Grid\Model;
use Dcat\Admin\Repositories\Repository;
use Exception;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Route;

class Dns extends Repository
{
    protected string $keyName = 'RecordId';

    /**
     * Notes   : 初始化阿里云客户端
     *
     * @Date   : 2024/8/1 下午2:03
     * @Author : <Jason.C>
     * @param  Aliyun  $aliyun
     * @return string
     * @throws ClientException
     */
    public static function initAliyun(Aliyun $aliyun): string
    {
        $aliyun->initClient();

        return self::class;
    }

    public function store(Form $form): bool
    {
        try {
            $attributes = $form->updates();

            Alidns::v20150109()
                ->addDomainRecord()
                ->withDomainName($attributes['DomainName'])
                ->withRR($attributes['RR'])
                ->withType($attributes['Type'])
                ->withValue($attributes['Value'])
                ->withTTL($attributes['TTL'])
                ->withLine($attributes['Line'])
                ->request();

            return true;
        } catch (Exception) {
            return false;
        }
    }

    /**
     * @throws ClientException
     * @throws ServerException
     */
    public function edit(Form $form): array|Arrayable
    {
        $id = $form->builder()->getResourceId();

        return Alidns::v20150109()
            ->describeDomainRecordInfo()
            ->withRecordId($id)
            ->request()
            ->get();
    }

    /**
     * @throws ClientException
     * @throws ServerException
     */
    public function get(Model $model): array|Collection|LengthAwarePaginator
    {
        $currentPage = $model->getCurrentPage();
        $perPage = $model->getPerPage();

        $route = Route::current();
        $domain = $route->parameter('domain');

        $res = Alidns::v20150109()
            ->describeDomainRecords()
            ->withPageNumber($currentPage)
            ->withPageSize($perPage)
            ->withDomainName($domain)
            ->request();

        return $model->makePaginator(
            $res->get('TotalCount'),
            $res->get('DomainRecords.Record')
        );
    }

    public function updating(Form $form): array
    {
        return [];
    }

    public function update(Form $form): bool
    {
        try {
            $id = $form->builder()->getResourceId();
            $attributes = $form->updates();

            Alidns::v20150109()
                ->updateDomainRecord()
                ->withRecordId($id)
                ->withRR($attributes['RR'])
                ->withType($attributes['Type'])
                ->withValue($attributes['Value'])
                ->withTTL($attributes['TTL'])
                ->withLine($attributes['Line'])
                ->request();

            return true;
        } catch (Exception) {
            return false;
        }
    }

    public function deleting(Form $form): array
    {
        return [];
    }

    /**
     * @throws ClientException
     * @throws ServerException
     */
    public function delete(Form $form, array $deletingData): bool
    {
        $ids = $form->builder()->getResourceId();
        $ids = explode(',', $ids);

        foreach ($ids as $id) {
            Alidns::v20150109()
                ->deleteDomainRecord()
                ->withRecordId($id)
                ->request();
        }

        return true;
    }
}
